<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-success"
                   plain
                   v-if="permission.cityworkAssess_examineCheck"
                   @click="handleExamineCheck">生成月度考核
        </el-button>
      </template>
      <template slot-scope="scope" slot="menu">
        <el-button
          type="text"
          icon="el-icon-video-play"
          size="small"
          @click.stop="handleExamine(scope.row,scope.index)"
          v-if="scope.row.status==0"
        >考核
        </el-button>
        <el-button
          type="text"
          icon="el-icon-circle-plus-outline"
          size="small"
          @click.stop="handleExamineDetail(scope.row,scope.index)"
          v-if="scope.row.status==1"
        >详情
        </el-button>
      </template>
    </avue-crud>
     <!-- 生成考核 -->
    <el-dialog title="生成月度考核数据"
                   append-to-body
                   :visible.sync="checkBoxShow"
                   width="35%">
    <el-form ref="CheckForm" :rules="rules" :model="checkForm" label-width="180px" size="mini">
     <el-card shadow="hover">
        <el-row>
          <el-col :span="24">
            <el-form-item label="考核周期：" prop="inspectionDate">
               <el-date-picker   type="month" value-format="yyyy-MM" v-model="checkForm.inspectionDate"/>
            </el-form-item>
          </el-col>
        </el-row>
         <el-row>
              <el-form-item class="align-text:center">
                <el-button type="primary" @click="handleCheckSave('CheckForm')">保存</el-button>
                <el-button @click="handleCheckCancel('CheckForm')">关闭</el-button>
              </el-form-item>
         </el-row>
     </el-card>
    </el-form>
    </el-dialog>
     <!-- 考核 -->
    <el-dialog title="部门考核"
                   append-to-body
                   :visible.sync="examineBoxShow"
                   width="70%">
    <el-form ref="ExamineForm" :rules="rules" :model="examineForm" label-width="180px" size="mini">
     <el-card shadow="hover">
        <el-row>
          <el-col :span="12">
            <el-form-item label="考核周期：">
               {{ examineForm.inspectionDate }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="考核部门：">
              {{ examineForm.deptName }}
            </el-form-item>
          </el-col>
          <el-col :span="12" v-show="false">
            <el-form-item label="总分：">
              {{ examineForm.total }}
            </el-form-item>
          </el-col>
          <el-col :span="12" v-show="false">
            <el-form-item label="id：">
              {{ examineForm.id }}
            </el-form-item>
          </el-col>
          <el-col :span="12" v-show="false">
            <el-form-item label="details：">
              {{ examineForm.details }}
            </el-form-item>
          </el-col>
          <el-table
                  :data="examineData"
                  border
                  :summary-method="getSummaries"
                  show-summary
                  style="width: 100%">
                  <el-table-column
                    prop="kpiType"
                    label="考核类型"
                    min-width="10%">
                 </el-table-column>
                 <el-table-column prop="typeName"  label="考核名称" min-width="60%">
                    <template slot-scope="scope">
                            <a @click="handleKipCheck(scope.row,'examineForm')" style="color:blue;cursor:pointer">{{scope.row.typeName}}</a>
                    </template>
                 </el-table-column>
                 <el-table-column
                    prop="points"
                    label="计算得分"
                    min-width="10%">
                 </el-table-column>
                 <el-table-column
                    prop="totalPoint"
                    label="最大分值"
                    min-width="10%">
                 </el-table-column>
                 <el-table-column
                    prop="realPoint"
                    label="最终得分"
                    min-width="10%">
                 </el-table-column>
          </el-table>
        </el-row><br/>
         <el-row>
              <el-form-item class="align-text:center">
                <el-button type="primary" @click="handleExamineSave('ExamineForm')">确定</el-button>
                <el-button @click="handleExamineCancel('ExamineForm')">关闭</el-button>
              </el-form-item>
         </el-row>
     </el-card>
    </el-form>
    </el-dialog>
     <!-- 考核详情 -->
    <el-dialog title="部门考核详情"
                   append-to-body
                   :visible.sync="detailBoxShow"
                   width="70%">
    <el-form ref="DetailForm" :rules="rules" :model="detailForm" label-width="180px" size="mini">
     <el-card shadow="hover">
        <el-row>
          <el-col :span="12">
            <el-form-item label="考核周期：">
               {{ detailForm.inspectionDate }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="考核部门：">
              {{ detailForm.deptName }}
            </el-form-item>
          </el-col>
           <el-col :span="12" v-show="false">
            <el-form-item label="id：">
              {{ examineForm.id }}
            </el-form-item>
          </el-col>
          <el-table
                  :data="examineData"
                  border
                  :summary-method="getSummaries2"
                  show-summary
                  style="width: 100%">
                  <el-table-column
                    prop="kpiType"
                    label="考核类型"
                    min-width="10%">
                 </el-table-column>
                 <el-table-column prop="typeName" label="考核名称" min-width="60%">
                    <template slot-scope="scope">
                            <a @click="handleKipCheck(scope.row,'detailForm')" style="color:blue;cursor:pointer">{{scope.row.typeName}}</a>
                    </template>
                 </el-table-column>
                 <!-- <el-table-column
                    prop="points"
                    label="计算得分"
                    min-width="10%">
                 </el-table-column> -->
                 <el-table-column
                    prop="totalPoint"
                    label="最大分值"
                    min-width="10%">
                 </el-table-column>
                 <el-table-column
                    prop="realPoint"
                    label="最终得分"
                    min-width="10%">
                 </el-table-column>
          </el-table>
        </el-row><br/>
         <el-row>
              <el-form-item class="align-text:center">
                <el-button @click="handleDetailCancel('DetailForm')">关闭</el-button>
              </el-form-item>
         </el-row>
     </el-card>
    </el-form>
    </el-dialog>
      <!-- 单项得分明细 -->
    <el-dialog title="单项得分明细"
                   append-to-body
                   :visible.sync="singleBoxShow"
                   width="70%">
    <el-form ref="SingleForm" :rules="rules" :model="singleForm" label-width="180px" size="mini">
     <el-card shadow="hover">
       <el-row>
          <el-table
                  :data="singleData"
                  border
                  style="width: 100%">
                  <el-table-column
                    prop="id"
                    label="工单号"
                    min-width="30%">
                 </el-table-column>
                 <el-table-column
                    prop="singlePoint"
                    label="得分"
                    min-width="10%">
                 </el-table-column>
                 <el-table-column
                    prop="reason"
                    label="说明"
                    min-width="60%">
                 </el-table-column>
          </el-table>
        </el-row>
        <br/>
         <el-row>
              <el-form-item class="align-text:center">
                <el-button @click="handleSingleCancel()">关闭</el-button>
              </el-form-item>
         </el-row>
     </el-card>
    </el-form>
    </el-dialog>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove,checkSave,getListByDeptAndMonth,examineSave,getListDetailMonth,getKpiChekDetail} from "@/api/govsvc/cityworkAssess";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        form: {},
        examineForm: {},
        detailForm: {},
        checkForm: {},
        singleForm: {},
        checkBoxShow: false,
        examineBoxShow: false,
        detailBoxShow: false,
      singleBoxShow: false,
      //计算考核分数
        examineData:[],
        singleData: [],
         rules: {
          answerPhone: [
            { required: true, message: '请输入考核周期', trigger: 'blur' },
          ],
          },
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          indexLabel:'序号',
          height: 'auto',
          calcHeight: 30,
          dialogWidth: 850,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: false,
          addBtn: false,
          editBtn: false,
          delBtn: false,
          selection: true,
          excelBtn: false,
          dialogClickModal: false,
          column: [
            {
              label: "主键ID",
              prop: "id",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: true,
            },
             {
              label: "部门",
              prop: "deptName",
              type: "input",
            },
            {
              label: "总分",
              prop: "totalPoint",
              type: "input",
            },
            {
              label: "考核周期",
              prop: "inspectionDate",
              type: "month",
              search: true,
              format: "yyyy-MM",
              valueFormat: "yyyy-MM",
            },
            {
              label: "考核结果详情",
              prop: "details",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: true,
            },
           
            {
              label: "状态",
              prop: "status",
              type: "input",
              hide: true,
            },
            
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.cityworkAssess_add, false),
          viewBtn: this.vaildData(this.permission.cityworkAssess_view, false),
          delBtn: this.vaildData(this.permission.cityworkAssess_delete, false),
          editBtn: this.vaildData(this.permission.cityworkAssess_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      //table 合计行
      getSummaries(param) {
        const { columns, data } = param;
        const sums = [];
        // console.log(columns, data, "columns,data");
        columns.forEach((column, index) => {
          if (index === 0) {
            sums[index] = "合计";
          }
          // index >= 3 || index <= 11 是你需要计算的列
          // 当然也可以指定一个或者多个列
          // index === 2 || index === 3 这样值求和第二个与第三个列
          else if (index === 4) {
            const values = data.map((item) => Number(item[column.property]));
            if (!values.every((value) => isNaN(value))) {
              sums[index] = values.reduce((prev, curr) => {
                const value = Number(curr);
                if (!isNaN(value)) {
                  return prev + curr;
                }
              }, 0);
            }
          } else {
            sums[index] = "";
          }
        });
        return sums;
      },
      getSummaries2(param) {
        const { columns, data } = param;
        const sums = [];
        // console.log(columns, data, "columns,data");
        columns.forEach((column, index) => {
          if (index === 0) {
            sums[index] = "合计";
          }
          // index >= 3 || index <= 11 是你需要计算的列
          // 当然也可以指定一个或者多个列
          // index === 2 || index === 3 这样值求和第二个与第三个列
          else if (index === 3) {
            const values = data.map((item) => Number(item[column.property]));
            if (!values.every((value) => isNaN(value))) {
              sums[index] = values.reduce((prev, curr) => {
                const value = Number(curr);
                if (!isNaN(value)) {
                  return prev + curr;
                }
              }, 0);
            }
          } else {
            sums[index] = "";
          }
        });
        return sums;
      },
      rowSave(row, done, loading) {
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
     //弹出层生成考核
      handleExamineCheck(){
        this.checkBoxShow = true;
      },
      //生成考核保存
      handleCheckSave(form,done,loading){
          checkSave(this.checkForm.inspectionDate).then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "保存成功!"
            });
            this.checkBoxShow = false;
            this.onLoad(this.page);
          }, error => {
            loading();
            window.console.log(error);
          });
      },
      //部门考核弹出层
      handleExamine(row){
        this.examineForm.inspectionDate = row.inspectionDate;
        this.examineForm.deptName = row.deptName;
        this.examineForm.id = row.id;
        getListByDeptAndMonth(row.id).then(res =>{
            console.log(res.data.data);
            this.examineData = res.data.data.list;
            this.examineForm.total = res.data.data.total;
            this.examineForm.details = res.data.data.details;
            this.examineBoxShow = true;
        });
       

      },
      //保存部门考核数据
      handleExamineSave(form,done,loading) {
        console.log(this.examineForm.id);
                  examineSave(this.examineForm.id,this.examineForm.total,this.examineForm.details).then(() => {
                    this.onLoad(this.page);
                    this.$message({
                      type: "success",
                      message: "保存成功!"
                    });
                    this.examineBoxShow = false;
                    this.onLoad(this.page);
                  }, error => {
                    loading();
                    window.console.log(error);
                  });
      },//保存完成
      //考核详情弹出
      handleExamineDetail(row){
        this.detailForm.inspectionDate = row.inspectionDate;
        this.detailForm.deptName = row.deptName;
        this.detailForm.id = row.id;
        getListDetailMonth(row.id).then(res =>{
            console.log(res.data.data);
            this.examineData = res.data.data;
            this.detailBoxShow = true;
        });
      },
      //生成考核弹出窗关闭
       handleCheckCancel(FormName){
        this.checkBoxShow = false;
        this.$refs[FormName].resetFields();
      },
      //点击考核内容列事件
      handleKipCheck(row,FormName){
          var id = "";
          if(FormName=='detailForm'){
             id = this.detailForm.id;
          }else{
             id = this.examineForm.id;
          }
          var kpiType = row.kpiType;
          
          getKpiChekDetail(id,kpiType).then(res => {
              this.singleBoxShow = true;
              this.singleData = res.data.data;

          });
      },
      //部门考核弹弹出窗口关闭
      handleExamineCancel(FormName){
        this.examineBoxShow = false;
        this.$refs[FormName].resetFields();
      },
       //考核详情弹出窗口关闭
      handleDetailCancel(FormName){
        this.detailBoxShow = false;
        this.$refs[FormName].resetFields();
      },
      //分享得分明细
      handleSingleCancel(){
        this.singleBoxShow = false;
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
